<#assign className = table.className>
<#assign classNameLower = className?uncap_first>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="${basepackage}.dao.${namespace}.${className}Dao">

    <resultMap id="${className}Map" type="${basepackage}.model.${className}">
        <#list table.columns as column>
        <result property="${column.columnNameLower}" column="${column.sqlName}"/>
		</#list>
    </resultMap>

	<sql id="columns">
        <#list table.columns as column>${column.sqlName} <#if column_has_next>,</#if></#list>
	</sql>

    <sql id="where">
        <where>
            1 = 1
        <#list table.columns as column>
            <if test="${column.columnNameLower} !=''">
                and ${column.sqlName} = #${column.columnNameLower}#
            </if>
        </#list>
        </where>
    </sql>

    <insert id="insert">
        INSERT INTO ${table.sqlName} (
        <#list table.columns as column>
        	${column.sqlName} <#if column_has_next>,</#if>
        </#list>
        ) VALUES (
        <#list table.columns as column>
        	#${column.columnNameLower}# <#if column_has_next>,</#if>
        </#list>
        )
    </insert>

    <update id="updateById" >
        UPDATE ${table.sqlName} SET
	        <#list table.notPkColumns as column>
	        ${column.sqlName} = #${column.columnNameLower}# <#if column_has_next>,</#if>
	        </#list>
        WHERE
        	<#list table.compositeIdColumns as column>
	        ${column.sqlName} = #${column.columnNameLower}# <#if column_has_next>,</#if>
	        </#list>
    </update>

    <delete id="deleteById">
        DELETE FROM ${table.sqlName} WHERE
        <#list table.compositeIdColumns as column>
        ${column.sqlName} = #${column.columnNameLower}# <#if column_has_next>,</#if>
		</#list>
    </delete>

    <select id="selectById" resultMap="${className}Map">
	    SELECT <include refid="columns"/>
	        FROM ${table.sqlName}
	        WHERE
        <#list table.compositeIdColumns as column>
            ${column.sqlName} = #${column.columnNameLower}# <#if column_has_next>,</#if>
        </#list>
    </select>

    <select id="pageCount" resultType="java.lang.Integer">
        SELECT count(*) FROM ${table.sqlName} <include refid="where"/>
    </select>

    <select id="selectPage" resultMap="${className}Map">
        SELECT <include refid="columns"/> FROM ${table.sqlName}
        <include refid="where"/>
        limit #page#,#limit#
    </select>

</mapper>

